<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col v-if="userInfo.userId === '1'" :span="4">
                <sys-org-tree
                    @treeClick="treeClick"
                />
            </el-col>
            <el-col :span="userInfo.userId === '1'? 20 : 24" :xs="24">
                <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="120px"
                         size="small">
                    <el-form-item label="科室分组名称" prop="deptGroupName">
                        <el-input
                            v-model="queryParams.deptGroupName"
                            clearable
                            placeholder="请输入科室分组名称"
                            @keyup.enter.native="handleQuery"
                        />
                    </el-form-item>
                    <el-form-item>
                        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button
                            v-hasPermi="['system:deptGroup:add']"
                            icon="el-icon-plus"
                            plain
                            size="mini"
                            type="primary"
                            @click="handleAdd"
                        >新增
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            v-hasPermi="['system:deptGroup:edit']"
                            :disabled="single"
                            icon="el-icon-edit"
                            plain
                            size="mini"
                            type="success"
                            @click="handleUpdate"
                        >修改
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            v-hasPermi="['system:deptGroup:remove']"
                            :disabled="multiple"
                            icon="el-icon-delete"
                            plain
                            size="mini"
                            type="danger"
                            @click="handleDelete"
                        >删除
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            v-hasPermi="['system:deptGroup:export']"
                            icon="el-icon-download"
                            plain
                            size="mini"
                            type="warning"
                            @click="handleExport"
                        >导出
                        </el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>

                <el-table v-loading="loading" :data="deptGroupList" @selection-change="handleSelectionChange">
                    <el-table-column align="center" type="selection" width="55"/>
                    <!--                    <el-table-column label="科室分组" align="center" prop="deptGroupId" v-if="true"/>-->
                    <el-table-column align="left" label="科室分组名称" prop="deptGroupName"/>
                    <el-table-column align="center" label="机构id" prop="orgId"/>
                    <el-table-column align="center" label="显示顺序" prop="orderNum"/>
                    <el-table-column align="center" label="备注" prop="remark"/>
                    <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
                        <template slot-scope="scope">
                            <el-button
                                v-hasPermi="['system:deptGroup:edit']"
                                icon="el-icon-edit"
                                size="mini"
                                type="text"
                                @click="handleUpdate(scope.row)"
                            >分配科室
                            </el-button>
                            <el-button
                                v-hasPermi="['system:deptGroup:edit']"
                                icon="el-icon-user"
                                size="mini"
                                type="text"
                                @click="handleAuthUser(scope.row)"
                            >分配人员
                            </el-button>
                            <el-button
                                v-hasPermi="['system:deptGroup:remove']"
                                icon="el-icon-delete"
                                size="mini"
                                type="text"
                                @click="handleDelete(scope.row)"
                            >删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination
                    v-show="total>0"
                    :limit.sync="queryParams.pageSize"
                    :page.sync="queryParams.pageNum"
                    :total="total"
                    @pagination="getList"
                />
            </el-col>
        </el-row>
        <!-- 添加或修改科室分组对话框 -->
        <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="40%">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="科室分组名称" prop="deptGroupName">
                                    <el-input v-model="form.deptGroupName" placeholder="请输入科室分组名称"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="显示顺序" prop="orderNum">
                                    <el-input v-model="form.orderNum" placeholder="请输入显示顺序" type="number"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="备注" prop="remark">
                                    <el-input v-model="form.remark" :autosize="{ minRows: 4, maxRows: 8}"
                                              placeholder="请输入内容" type="textarea"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-col>
                    <el-col :span="12">
                        <dept-tree
                            :key="deptTreeKey"
                            ref="deptTree"
                            :default-checked-keys="defaultCheckedKeys"
                            :excess-height="600"
                            :org-id="queryParams.orgId"
                            :show-checkbox="true"
                            @treeClick="treeClick"
                        />
                    </el-col>
                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addDeptGroup,
    delDeptGroup,
    getDeptGroup,
    listDeptGroup,
    selectPageDeptGroup,
    updateDeptGroup
} from "@/api/system/deptGroup";

export default {
    name: "DeptGroup",
    data () {
        return {
            // 登录用户信息
            userInfo: this.$store.state.user.userInfo,
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 科室分组表格数据
            deptGroupList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                deptGroupName: undefined,
                orgId: undefined,
                orderNum: undefined,
            },
            // 表单参数
            form: {},
            // 已选中的科室树节点
            defaultCheckedKeys: [],
            deptTreeKey: new Date().getTime(),
            // 表单校验
            rules: {
                deptGroupId: [
                    {required: true, message: "科室分组不能为空", trigger: "blur"}
                ],
                deptGroupName: [
                    {required: true, message: "科室分组名称不能为空", trigger: "blur"}
                ],
                orderNum: [
                    {required: true, message: "显示顺序不能为空", trigger: "blur"}
                ],
            }
        };
    },
    created () {
        if (this.userInfo.orgId) {
            this.queryParams.orgId = this.userInfo.orgId
            this.getList()
        }
    },
    methods: {
        /** 左侧科室树节点点击事件 */
        treeClick (obj) {
            // this.queryParams.orgId = obj.id
            // this.getList()
        },
        /** 查询科室分组列表 */
        getList () {
            if (!this.queryParams.orgId) {
                return this.$modal.msgError("请先选择左侧所属机构后查询")
            }
            this.loading = true;
            selectPageDeptGroup(this.queryParams).then(response => {
                this.deptGroupList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel () {
            this.defaultCheckedKeys = []
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset () {
            this.form = {
                deptGroupId: undefined,
                deptGroupName: undefined,
                orderNum: undefined,
                delFlag: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                remark: undefined,
                orgId: this.queryParams.orgId
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange (selection) {
            this.ids = selection.map(item => item.deptGroupId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        // 检测orgId是否存在
        // checkOrgIdExist () {
        //     if (!this.userInfo.orgId && !this.queryParams.orgId) {
        //         return this.$modal.msgError("请先选择左侧所属机构后查询")
        //     }
        // },
        /** 新增按钮操作 */
        handleAdd () {
            if (!this.userInfo.orgId && !this.queryParams.orgId) {
                return this.$modal.msgError("请先选择左侧所属机构后查询")
            }
            this.defaultCheckedKeys = []
            this.deptTreeKey = new Date().getTime()
            this.reset();
            this.open = true;
            this.title = "添加科室分组";
        },
        /** 分配按钮操作 */
        handleUpdate (row) {
            if (!this.userInfo.orgId && !this.queryParams.orgId) {
                return this.$modal.msgError("请先选择左侧所属机构后查询")
            }
            this.loading = true;
            this.reset();
            const deptGroupId = row.deptGroupId || this.ids
            getDeptGroup(deptGroupId).then(response => {
                let data = response.data;
                this.loading = false;
                this.form = data;
                this.defaultCheckedKeys = data.deptIds
                this.open = true;
                this.title = "修改科室分组";
            });
        },
        /** 分配用户操作 */
        handleAuthUser: function (row) {
            const deptGroupId = row.deptGroupId;
            const businessType = '2';
            this.$router.push("/system/role-auth/user/" + this.queryParams.orgId + "/" + deptGroupId + "/" + businessType);
        },
        /** 提交按钮 */
        submitForm () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    this.form.deptIds = this.$refs.deptTree.getCheckedKeys()
                    if (this.form.deptGroupId != null) {
                        updateDeptGroup(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        addDeptGroup(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const deptGroupIds = row.deptGroupId || this.ids;
            this.$modal.confirm('是否确认删除科室分组编号为"' + deptGroupIds + '"的数据项？').then(() => {
                this.loading = true;
                return delDeptGroup(deptGroupIds);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            }).finally(() => {
                this.loading = false;
            });
        },
        /** 导出按钮操作 */
        handleExport () {
            this.download('system/deptGroup/export', {
                ...this.queryParams
            }, `deptGroup_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
